package com.kingdee.eas.EDCommonUtil;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Window;

import javax.swing.JLabel;
import javax.swing.JPanel;

import com.kingdee.bos.ctrl.swing.KDDialog;
import com.kingdee.bos.ctrl.swing.KDProgressBar;
import com.kingdee.bos.ctrl.swing.KDSeparator;
import com.kingdee.eas.base.permission.client.longtime.ILongTimeTask;
import com.kingdee.eas.base.permission.client.longtime.SwingWorker;
import com.kingdee.eas.util.client.EASResource;
import com.kingdee.eas.util.client.ExceptionHandler;

public class EDCommonlongTimeDialog extends KDDialog {
	private static final String RESOURCE = "com.kingdee.eas.base.permission.PermissionResource";
	private ILongTimeTask longTimeTask;
	private static final Color bottomBgColor = new Color(230, 230, 230);
	private Object result;
	private Window parent;

	public EDCommonlongTimeDialog(Frame frame,String title) {
		super(frame, title , true);
		longTimeTask = null;
		result = null;
		parent = null;
		parent = frame;
	}

	public EDCommonlongTimeDialog(Dialog dialog,String title) {
		super(dialog, title , true);
		longTimeTask = null;
		result = null;
		parent = null;
		parent = dialog;
	}

	public void setLongTimeTask(ILongTimeTask longTimeTask) {
		this.longTimeTask = longTimeTask;
	}

	protected void dialogInit() {
		super.dialogInit();
		setResizable(false);
		setDefaultCloseOperation(0);
		setLocationRelativeTo(null);
		JPanel contentPane = (JPanel) getContentPane();
		contentPane.setLayout(null);
		Dimension bgPanelDimension = new Dimension(363, 96);
		int normalBottomHeight = 42;
		int advanceBottomHeight = 202;
		contentPane.setPreferredSize(new Dimension(bgPanelDimension.width, bgPanelDimension.height + 42));
		contentPane.setBackground(new Color(0xffffff));
		JPanel pnlBottom = new JPanel();
		pnlBottom.setLayout(null);
		pnlBottom.setBounds(0, bgPanelDimension.height, bgPanelDimension.width, 202);
		contentPane.add(pnlBottom);
		pnlBottom.setBackground(bottomBgColor);
		Graphics2D g2d = (Graphics2D) pnlBottom.getGraphics();
		if (g2d != null) {
			GradientPaint gradient = new GradientPaint(0.0F, 0.0F, new Color(0xffffff), 363F, 96F, new Color(0xb4b4b4));
			g2d.setPaint(gradient);
		}
		KDSeparator sp = new KDSeparator();
		sp.setBounds(0, 0, bgPanelDimension.width, 2);
		pnlBottom.add(sp);
		JLabel text = new JLabel();
		text.setBounds(44, 29, 279, 20);
		KDProgressBar progressBar = new KDProgressBar();
		progressBar.setStringPainted(false);
		progressBar.setIndeterminate(true);
		getContentPane().add(text, null);
		getContentPane().add(progressBar, null);
		progressBar.setBounds(new Rectangle(42, 50, 279, 12));
		pack();
		setLocationRelativeTo(null);
	}

	public void show() {
		SwingWorker longTimeTaskWorker = new SwingWorker() {
			public Object construct() {
				try {
					result = longTimeTask.exec();
					return result;
				} catch (Exception ex) {
					ex.printStackTrace();
					ExceptionHandler.handle(parent, ex);
					dispose();
					return null;
				}
			}

			public void finished() {
				dispose();
				if (result != null)
					try {
//						Thread.sleep(500L);
						longTimeTask.afterExec(result);
					} catch (Exception e) {
						ExceptionHandler.handle(parent, e);
					}
			}
		};
		longTimeTaskWorker.start();
		setCursor(this, Cursor.getPredefinedCursor(3));
		super.show();
		setCursor(this, Cursor.getPredefinedCursor(0));
	}

	private void setCursor(Component cp, Cursor cursor) {
		cp.setCursor(cursor);
		if (cp instanceof Container) {
			Container cc = (Container) cp;
			int i = 0;
			for (int n = cc.getComponentCount(); i < n; i++) {
				Component curComponent = cc.getComponent(i);
				setCursor(curComponent, cursor);
			}

		}
	}

}
